package qc.module.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.exception.QCPromptException;
import qc.common.core.utils.AdminDivisionUtils;
import qc.module.platform.dto.division.AdminDivisionInfoDto;
import qc.module.platform.dto.division.AdminDivisionQueryConditionDto;
import qc.module.platform.entity.QcAdminDivisions;
import qc.module.platform.mapper.AdminDivisionMapper;
import qc.module.platform.repository.QcAdminDivisionRepository;

import java.util.List;

/**
 * AdmiDivisionService
 *
 * @author QuCheng Tech
 * @create 2023/02/08
 */
@Service
public class AdminDivisionService {
    private QcAdminDivisionRepository repository;

    @Autowired
    public void setQcAdminDivisionRepository(QcAdminDivisionRepository repository) {
        this.repository = repository;
    }

    /**
     * 根据查询条件查询相关行政区划信息,输入信息为空，返回所有行政区划信息
     *
     * @param dto AdminDivisionQueryConditionDto
     * @return 行政区划信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    public List<AdminDivisionInfoDto> query(AdminDivisionQueryConditionDto dto) throws Exception {
        LambdaQueryWrapper<QcAdminDivisions> wrapper = new LambdaQueryWrapper<>();

        //指定查询字段，不查询geos字段
        wrapper.select(QcAdminDivisions::getCode, QcAdminDivisions::getName, QcAdminDivisions::getLevel, QcAdminDivisions::getLng, QcAdminDivisions::getLat);

        if (dto != null) {
            //如果输入的geos字段是true，即需要查询边界，则查询全部列的信息
            if (dto.getGeos() != null && dto.getGeos())
                wrapper.select(QcAdminDivisions::getCode, QcAdminDivisions::getName, QcAdminDivisions::getLevel, QcAdminDivisions::getLng, QcAdminDivisions::getLat, QcAdminDivisions::getGeos);

            if (dto.getLevel() != null)
                wrapper.eq(QcAdminDivisions::getLevel, dto.getLevel());
            //指定了父级编码
            if (!StringUtils.isBlank(dto.getParent())) {
                //判断输入的编码是否是12位数字组成
                if (!AdminDivisionUtils.isNum(dto.getParent()))
                    throw new QCPromptException("请输入正确的12位数字编码");

                int level = AdminDivisionUtils.getDivisionLevel(dto.getParent());
                String startCode = AdminDivisionUtils.getDivisionStartCode(dto.getParent(), level);

                wrapper.likeRight(QcAdminDivisions::getCode, startCode).ne(QcAdminDivisions::getLevel, level);
            }
            //关键字使用like，放在where条件最后
            if (!StringUtils.isBlank(dto.getWords()))
                wrapper.and(k -> k.like(QcAdminDivisions::getName, dto.getWords()));
        }
        //根据等级排序,省级~村委会级分别是1~5，所以使用升序
        wrapper.orderByAsc(QcAdminDivisions::getLevel);

        List<QcAdminDivisions> list = repository.selectList(wrapper);

        if (list.size() > 0x0)
            return AdminDivisionMapper.MAPPER.toDtoList(list);

        return null;
    }

    /**
     * 获取指定等级下一级行政区划,必须指定父级编码
     *
     * @param dto AdminDivisionQueryConditionDto
     * @return 行政区划等级信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    public List<AdminDivisionInfoDto> getChilds(AdminDivisionQueryConditionDto dto) throws Exception {
        if (dto == null)
            throw new QCPromptException("传入对象为空");
        if (StringUtils.isBlank(dto.getParent()) || !AdminDivisionUtils.isNum(dto.getParent()))
            throw new QCPromptException("请输入正确的12位数字编码");

        //AdminDivisionUtils.getDivisionLevel()方法得到父级编码等级，下一级编码等级需要+1，将编码等级置入dto中的level字段
        dto.setLevel(AdminDivisionUtils.getDivisionLevel(dto.getParent()) + 1);

        return query(dto);
    }
}
